using System;
using System.Collections.Generic;
using System.Text;
using Castle.ActiveRecord;

namespace Scrummer.Models
{
	[ActiveRecord]
	public class Backlog : AbstractModel<Backlog>
	{
		private IList<WorkItem> workItems;

		[HasMany]
		public IList<WorkItem> WorkItems
		{
			get
			{
				if (workItems == null)
					workItems = new List<WorkItem>();

				return workItems;
			}
			protected set { workItems = value; }
		}

		public double Estimate
		{
			get
			{
				double sum = 0;
				foreach (WorkItem item in workItems)
					sum += item.Estimate;
				return sum;
			}
		}

		public double DetailEstimate
		{
			get
			{
				double sum = 0;
				foreach (WorkItem item in WorkItems)
					sum += item.DetailEstimate;
				return sum;
			}
		}

		public double Effort
		{
			get
			{
				double sum = 0;
				foreach (WorkItem item in WorkItems)
					sum += item.Effort;
				return sum;
			}
		}

		public double EffortRemaining
		{
			get
			{
				double sum = 0;
				foreach (WorkItem item in WorkItems)
					sum += item.EffortRemaining;
				return sum;
			}
		}

		public void Add(WorkItem workItem)
		{
			if(workItem == null)
				throw new ArgumentNullException("Cannot add a null WorkItem");

			WorkItems.Add(workItem);
		}

		protected override void OnCreating()
		{
			if (workItems != null)
			  foreach (WorkItem workItem in workItems)
			    if (workItem.IsNew)
			      workItem.Create();

			base.OnCreating();
		}

        //private IList<BacklogItem> backlogItems;
        //private IList<Defect> defects;

        //[HasMany]
        //public IList<Defect> Defects
        //{
        //    get
        //    {
        //        if (defects == null)
        //            defects = new List<Defect>();
		 
        //        return defects;
        //    }
        //    set { defects = value; }
        //}

        //[HasMany]
        //public IList<BacklogItem> BacklogItems
        //{
        //    get
        //    {
        //        if (backlogItems == null)
        //            backlogItems = new List<BacklogItem>();

        //        return backlogItems;
        //    }
        //    set { backlogItems = value; }
        //}
    }
}
